//
//  StartWriteHomeworkCell.m
//  ZPIOSEducation
//
//  Created by 董伟1 on 17/1/12.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "StartWriteHomeworkCell.h"
#import "HomeworkPictureCell.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@implementation StartWriteHomeworkCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

    self.placeholderLab.hidden = YES;
    self.numLab.hidden = YES;
    self.textView.delegate = self;
    self.picCollectionView.delegate = self;
    self.picCollectionView.dataSource = self;
    self.picCollectionView.scrollEnabled = NO;
    
    dataArray = [[NSMutableArray  alloc] init];
    isEdit = NO;
    
    [self.picCollectionView  registerNib:[UINib  nibWithNibName:@"HomeworkPictureCell" bundle:nil] forCellWithReuseIdentifier:@"HomeworkPictureCell"];
    
}

- (void)setContentCellWithArray:(NSArray  *)array  isEditing:(BOOL)editing
{
    [dataArray  removeAllObjects];
    [dataArray  addObjectsFromArray:array];
    isEdit = editing;
    [self.picCollectionView  reloadData];
    
    if (self.textView.text.length == 0 && isEdit) {
        self.placeholderLab.hidden = NO;
    }
}

- (void)deleteButtonClick:(id)sender
{
    UIButton  *tempBtn = (UIButton  *)sender;
    if ([self.writeDelegate  respondsToSelector:@selector(deletePictureWithIndex:)]) {
        [self.writeDelegate  deletePictureWithIndex:tempBtn.tag];
    }
}

#pragma mark - UICollectionView Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [dataArray  count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREENWIDTH - 46) / 3 - 0.5, 87);
}

- (UICollectionViewCell  *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"HomeworkPictureCell";
    HomeworkPictureCell  *cell = (HomeworkPictureCell  *)[collectionView  dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    NSInteger  row = [indexPath  row];
    NSDictionary  *tempDic = [dataArray  objectAtIndex:row];
    if ([[tempDic  objectForKey:@"type"] integerValue] == 2) {
        cell.pictureImv.image = [UIImage  imageWithData:[tempDic  objectForKey:@"data"]];
    }else {
        [cell.pictureImv  sd_setImageWithURL:[NSURL  URLWithString:[NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"data"]]] placeholderImage:[UIImage  imageNamed:@"Placeholder200_200"]];
    }
    if (isEdit) {
        cell.deleteBtn.hidden = NO;
    }else {
        cell.deleteBtn.hidden = YES;
    }
    
    cell.deleteBtn.tag = row;
    [cell.deleteBtn  addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeworkPictureCell  *cell = (HomeworkPictureCell  *)[collectionView  cellForItemAtIndexPath:indexPath];
    CGRect rect = [cell convertRect:cell.pictureImv.frame toView:self.window];
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    NSInteger  count = [dataArray  count];
    for (int i = 0; i<count; i++) {
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.firstFrame = rect;
        NSDictionary  *tempDic = [dataArray  objectAtIndex:i];
        if ([[tempDic  objectForKey:@"type"] integerValue] == 2) {
            photo.image = [UIImage  imageWithData:[tempDic  objectForKey:@"data"]];
            NSData *data=UIImageJPEGRepresentation(photo.image, 0);
            NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%lu.jpg",NSHomeDirectory(),(unsigned long)data.length];
            [data writeToFile:aPath atomically:YES];
            photo.url = aPath;
        }else {
            photo.url = [NSString  stringWithFormat:@"%@",[tempDic objectForKey:@"data"]]; // 图片路径
        }
        photo.srcImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Placeholder200_200"]]; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.isShowRight = NO;
    browser.photos = photos; // 设置所有的图片
    browser.currentPhotoIndex = indexPath.row;
    [browser show];
    
}

#pragma mark - UITextView Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    self.placeholderLab.hidden = YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length == 0) {
        self.placeholderLab.hidden = NO;
    }else {
        self.placeholderLab.hidden = YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger number = [textView.text length];
    if (number > 500) {
        self.numLab.hidden = NO;
        self.numLab.text = @"0";
        textView.text = [textView.text substringToIndex:500];
        return;
    }
    if (number > 449) {
        self.numLab.hidden = NO;
        self.numLab.text = [NSString  stringWithFormat:@"%ld",MAX(0, 500-number)];
    }else {
        self.numLab.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
